/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __PHOTONTRACER_H__
#define __PHOTONTRACER_H__

#include "photon_map.h"
#include "common/random_generator.h"
#include "math/ray.h"
#include "math/vector.h"
#include "common/scene.h"
#include "object/object.h"
#include "photon.h"

#include <boost/tuple/tuple.hpp>

// using namespace std; 

using namespace boost::tuples;

namespace Sirat {

namespace Photon_Mapping {

class Photontracer {
public:
	Photontracer(Common::Scene* _scene,const unsigned short _depth,const unsigned int _photons);
	~Photontracer(void);
	
	Photon_Map* get_photon_map(void);
	
	void trace(void);
	
	/**
	 * Generate n photons like reverse ray tracing.
	 * @param _point 
	 * @param _radius
	 * @param _n Number of photons to be generated.
	 * @param _current_size Number of photons already generated.
	 * @return Array of photons
	 */
	std::vector<tuple<Photon*,double> > reverse_trace(Object::Object* _object,const Math::Vector& _point,const double _radius,const unsigned int _n, const unsigned int _current_size);
	
private:
	void trace_rec(Object::Object* _object,const Math::Ray& _ray,const Math::Vector& _point,const Common::Color& _photon, const unsigned short _depth);
	Photon* calculate_photon(Object::Object*, const Math::Ray&, const Math::Vector&);
	Math::Ray reflected_ray(const Math::Ray&, const Math::Vector&, const Math::Vector&);
	
	Photon_Map photon_map;
	Common::Scene* scene;
	unsigned short depth;
	unsigned int photons;
	
	/**
	 * Pseudo-random generator.
	 */
	Common::Random_Generator random;
};

}

}

#endif
